package org.example.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Iterator;

/**
 * 原生的filter 一般作为中间件使用
 * 也可以像 servlet 一样去包装以下
 */
public class LogFilter extends HttpFilter {


    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        System.out.println("--------------------http start--------------------------");
        System.out.println(req.getMethod() +"------> "+req.getRequestURI());
        Iterator<String> iterator = req.getHeaderNames().asIterator();
        while (iterator.hasNext()){
            String name = iterator.next();
            System.out.println(name+":--->"+req.getHeader(name));
        }
        System.out.println("-----------------------http end----------------------------");
        chain.doFilter(req, res);
    }
}
